package com.ml.util;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

/**
 * window的Access数据库读取
 * @Author ZZY
 */
public class Accessdatabase {
    public static void main(String[] args) {
        test2();
    }

    public static void test1(){
        Connection con = null;
        ResultSet rst = null;
        Statement sta = null;
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String url = "jdbc:odbc:zzyaccess";//databaseName就是刚刚添加的数据源名称
            Properties pro = new Properties();//此方案解决中文乱码
            pro.setProperty("charSet","GB2312");
            pro.put( "charSet", "gbk" );
            pro.put( "user", "zzy" );
            pro.put( "password", "123456" );
            con = DriverManager. getConnection( url, pro );
            //con = DriverManager.getConnection(url, "zzy", "123456");//没有用户名和密码的时候直接为空
            sta = con.createStatement();


            // sta.executeUpdate() sta.execute();


            rst = sta.executeQuery("select * from zzy_fx");//demoTable为access数据库中的一个表名
            while(rst.next()){
                String title = rst.getString("title");
                System.out.println(rst.getString("name")+"--"+title);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(rst!=null){
                    rst.close();
                }
                if(sta!=null){
                    sta.close();
                }
                if(con!=null){
                    con.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }


    public static void test2() {
        Connection con = null;
        ResultSet rst = null;
        Statement sta = null;
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String url = "jdbc:rmi://192.168.70.100/jdbc:odbc:zzyaccess";//databaseName就是刚刚添加的数据源名称
            Properties pro = new Properties();//此方案解决中文乱码
            pro.setProperty("charSet","GB2312");
            pro.put( "charSet", "gbk" );
            pro.put( "user", "zzy" );
            pro.put( "password", "123456" );
            con = DriverManager. getConnection( url, pro );
            sta = con.createStatement();
            rst = sta.executeQuery("select * from zzy_fx");//demoTable为access数据库中的一个表名
            while(rst.next()){
                String title = rst.getString("title");
                System.out.println(rst.getString("name")+"--"+title);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(rst!=null){
                    rst.close();
                }
                if(sta!=null){
                    sta.close();
                }
                if(con!=null){
                    con.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }




    public void a2(){
       /* 第二种方法

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

        public class AccessJdbc {
            public static void main(String[] args) {
                Connection conn = null;
                Statement ps = null;
                ResultSet rs = null;
                try {
                    // Access注册驱动
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    // url地址(*.mdb, *.accdb)括号内容中间"逗号"后面要有空格
                    String url = "jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb, *.accdb)};DBQ=C:\\Users\\Administrator\\Desktop\\11.accdb";

                    conn = DriverManager.getConnection(url);

                    ps = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                            ResultSet.CONCUR_READ_ONLY);

                    rs = ps.executeQuery("select * from test");

                    while (rs.next()) {
                        System.out.println(rs.getString(1));
                    }
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        rs.close();
                        ps.close();
                        conn.close();
                    } catch (Exception e2) {
                        e2.printStackTrace();
                    }
                }
            }
        }

*/
    }

}
